package com.sun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.dto.InstoreDto;
import com.sun.dto.PurchaseDto;
import com.sun.entity.InstoreDetail;
import com.sun.service.InstoreDetailService;
import com.sun.service.InstoreService;
import com.sun.service.PurchaseHeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/3/24 16:20
 * @FileName: InstoreController
 * @Description: ""
 */
@RestController
@RequestMapping("/inStore")
public class InstoreController {

    @Autowired
    private PurchaseHeadService purchaseHeadService;
    @Autowired
    private InstoreService instoreService;

    @Autowired
    private InstoreDetailService instoreDetailService;

    /**
     * 根据条件查询对应的采购单信息
     *
     * @param userName
     * @param supplierName
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/list")
    public Result<List<PurchaseDto>> list(String userName, String supplierName, Integer status, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        List<PurchaseDto> purchaseDtos = purchaseHeadService.selectPurchaseDtoList(userName, supplierName, status, startTime, endTime);
        return Result.success(purchaseDtos);
    }

    /**
     * 新增入库单
     */
    @SystemCrmlog(title = "入库管理模块", type = "INSERT", description = "新增入库单")
    @PostMapping
    public Result<String> save(@RequestBody InstoreDto instoreDto) {
        instoreService.saveWithInstoreDetail(instoreDto);
        return Result.success("新增入库单成功");
    }

    /**
     * 修改入库单
     */
    @SystemCrmlog(title = "入库管理模块", type = "UPDATE", description = "修改入库单")
    @PutMapping
    public Result<String> update(@RequestBody InstoreDto instoreDto) {

        instoreService.updateWithInstoreDetail(instoreDto);

        return Result.success("入库单信息修改成功");
    }

    /**
     * 分页查询(可以根据审核员姓名、入库状态、入库类型、时间进行条件查询）
     */
    @GetMapping("/page")
    public Result<IPage> page(int pageNum, int pageSize, String userName,
                              Integer status, Integer type, String addr,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<InstoreDto> pageInfo = new Page<>(pageNum, pageSize);

        IPage<InstoreDto> inStoreDtoPage = instoreService.selectInstoreDtoListPage(pageInfo, userName, status, type, addr, startTime, endTime);

        return Result.success(inStoreDtoPage);
    }

    /**
     * 根据id查询入库单的相关信息
     */
    @GetMapping("/{id}")
    public Result<InstoreDto> get(@PathVariable Long id) {
        InstoreDto instoreDto = instoreService.getByIdWithInstoreDetail(id);
        return Result.success(instoreDto);
    }

    /**
     * 根据id删除入库单（可以批量删除）
     */
    @SystemCrmlog(title = "入库管理模块", type = "DELETE", description = "根据id删除入库单")
    @DeleteMapping("/{ids}")
    public Result<String> batchDelete(@PathVariable Long ids) {
        instoreService.batchDeleteByIds(Collections.singletonList(ids));
        return Result.success("成功删除入库单！");
    }

    /**
     * 修改入库单的状态为完成，并更新库存
     */
    @SystemCrmlog(title = "入库管理模块", type = "UPDATE", description = "修改入库单的状态为完成，并更新库存")
    @PostMapping("/status/{ids}")
    public Result<String> updateStatus(@PathVariable Long ids) {
        instoreService.updateStatusByIds(Collections.singletonList(ids));

        return Result.success("入库单状态已更改!");
    }

    //根据入库单详情id修改数量
    @SystemCrmlog(title = "入库管理模块", type = "UPDATE", description = "修改入库单中的药品数量")
    @PostMapping("/updateInstoreDetail")
    public Result<String> updatePurchaseDetail(@RequestBody Map<String, String> map) {
        InstoreDetail detail = instoreDetailService.getById(Long.valueOf(map.get("id")));
        detail.setNum(detail.getNum() - Integer.parseInt(map.get("num")));
        instoreDetailService.updateById(detail);
        return Result.success("修改成功");
    }
}
